<script>
	import config from '@/common/config.js'
	import MeetingWebSocket from '@/util/meetingWebSocket/MeetingWebSocket.js'

	export default {
		globalData: {
			meetingWebSocket: null
		},
		data() {
			return {
				networkAvailable: null,
			}
		},
		onLaunch: function() {
		this.connectWebSocket()
		this.initNetworkListener()
		},
		onShow: function() {

		},
		onHide: function() {

		},
		onUnload() {
			uni.offNetworkStatusChange((res)=>{
				console.log('取消监听网络',res)
			})
		},
		methods: {
			initNetworkListener() {
				// 监听网络变化
				uni.onNetworkStatusChange((res) => {
					this.networkAvailable = res.isConnected;
					if (!res.isConnected) {
						// 没有网络关闭连接
						uni.showToast({
							title:'网络连接断开，请检查网络',
							icon:'error',
							duration:2000
						})
						this.globalData.meetingWebSocket.close()
					}else{
						uni.showToast({
							title:'网络已连接，请刷新页面',
							icon:'success',
							duration:2000
						})
					}
				});
			},
			connectWebSocket() {
				//创建全局单例IM通讯对象
				this.globalData.meetingWebSocket = new MeetingWebSocket()

				let doctorId = uni.getStorageSync('doctorId')

				if (doctorId) {
					this.globalData.meetingWebSocket.init(config.meeting_websocke_url + doctorId)
				}
			},
			showToast(options) {
				uni.showToast({
					title: '',
					icon: 'none',
					mask: true,
					...options
				});
			},
			showModal(options) {
				return new Promise((resolve, reject) => {
					uni.showModal({
						title: '',
						content: '',
						showCancel: true,
						cancelText: '取消',
						cancelColor: '#999999',
						confirmText: '确定',
						confirmColor: '#007aff',
						...options,
						success: (res) => {
							resolve(res)
							if (res.confirm) {
								console.log('用户点击确定')
							} else if (res.cancel) {
								console.log('用户点击取消')
							}
						},
						fail: (error) => {
							reject(error)
						}
					});
				})
			},
			showLoading(options) {
				uni.showLoading({
					title: '正在加载',
					mask: true,
					...options
				});
			},
			hideLoading() {
				uni.hideLoading()
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";

	/*#ifdef !H5*/
	.page {
		width: 100vw;
		min-height: 100vh;
		background-color: $bg-color;
	}

	/*#endif*/
	/*#ifdef H5*/
	page {
		background-color: $bg-color;
	}

	/*#endif*/
	body {
		height: 100%;
	}

	.clamp-line-1 {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		/* 限制行数为3 */
		overflow: hidden;
	}

	.clamp-line-2 {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		/* 限制行数为3 */
		overflow: hidden;
	}

	.card-container {
		margin: 0 auto;
		padding: 0 0 32rpx 0;
		width: 686rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		background-color: #fff;

		.card-header-warpper {
			padding: 32rpx;

			.card-title {
				font-size: $ft-size-md;
				color: #333;
			}
		}
	}

	.placeholder-color {
		color: rgb(192, 196, 204)
	}

	.input-text {
		font-size: 30rpx;
	}
</style>